package com.softcomputer.rpt.ejb;

import com.softcomputer.rpt.entity.LogMessage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@Stateless
public class LogMessageSession implements Serializable {

    @Resource(mappedName = "jms/NotificationQueue")
    private Queue notificationQueue;
    @Resource(mappedName = "jms/NotificationQueueFactory")
    private ConnectionFactory notificationQueueFactory;
    @PersistenceContext(unitName = "Exchanger")
    private EntityManager em;
    private LogMessage message;
    private static final Log LOG = LogFactory.getLog(ExchangeSession.class);

    public void persist(Object object) {
        message = (LogMessage) object;
        try {
            em.persist(message);
        } catch (Exception exception) {
            LOG.error("Unable to persist message: " + exception.getMessage());
        }
    }

    public List<LogMessage> retrieve() {
        Query getAllMessagesQuery = em.createNamedQuery("LogMessage.findAll");
        try {
            return getAllMessagesQuery.getResultList();
        } catch (Exception exception) {
            LOG.error("Unable to retrieve log messages from entity manager: " + exception.getMessage());
            return new ArrayList<LogMessage>();
        }
    }
}
